package com.xgr.wonderful.activity;

import java.util.ArrayList;
import java.util.List;

import android.os.Bundle;
import android.support.v4.view.ViewPager;
import android.support.v4.view.ViewPager.OnPageChangeListener;
import android.view.View;
import android.view.ViewGroup;
import android.view.Window;
import android.view.View.OnClickListener;

import com.nostra13.universalimageloader.core.ImageLoader;
import com.polites.android.GestureImageView;
import com.viewpagerindicator.CirclePageIndicator;
import com.xgr.wonderful.activity.base.BaseActivity;
import com.xgr.wonderful.adapter.GalleryViewAdapter;
import com.xgr.wonderful.entity.Photo;
import com.xgr.wonderful.entity.Status;
import com.xgr.wonderful.utils.ToastFactory;
import com.xgr.wonderful.R;

/**
 * 如果想加入保存的功能 ，可以加入 actionbar 
 * @author lsj
 *
 */
public class GalleryActivity extends BaseActivity {
	private CirclePageIndicator mIndicator;
	private ViewPager mViewPager ;
	private Status status;
	private List<View > mViewLists = new ArrayList<View>();
	
	@Override
	protected void onCreate(Bundle bundle) {
		// TODO Auto-generated method stub
		super.onCreate(bundle);
	    requestWindowFeature(Window.FEATURE_NO_TITLE);
		setContentView(R.layout.activity_viewpager);
		status = (Status) getIntent().getSerializableExtra("data") ; 
		if (status == null){
			finish() ; //如果 是空，返回
		}
		mIndicator = (CirclePageIndicator)findViewById(R.id.circlepageindicator);
		mViewPager = (ViewPager )findViewById(R.id.viewpager);
		initViewPagers();

	}
	
	private void initViewPagers(){
		//实际上应该是这样 的,还要进行图片的加载 工作
		List<Photo>  imageList = status.getImageurl() ;
		if (imageList == null || imageList.size()==0){
			ToastFactory.getToast(mContext, "没有照片").show() ;
			finish() ;
		}
		for (int i =0 ;i<imageList.size();i++){
			ViewGroup.LayoutParams lp = new ViewGroup.LayoutParams(
					ViewGroup.LayoutParams.WRAP_CONTENT, 
					ViewGroup.LayoutParams.WRAP_CONTENT) ;
			
			//可以双击 放大的，开源
			GestureImageView gView = new GestureImageView(this ) ;
			
			gView.setLayoutParams(lp) ;
			ImageLoader.getInstance().displayImage(imageList.get(i).getUrl(), gView) ;
			
			gView.setOnClickListener(new OnClickListener() {
				
				@Override
				public void onClick(View v) {
					// TODO Auto-generated method stub
					finish();
				}
			}) ;
			//没有了actionbar ,使用这个返回，依微信的
			mViewLists.add(gView) ;
		}		
		GalleryViewAdapter myPagerAdapter = new GalleryViewAdapter(mViewLists);
		mViewPager.setAdapter(myPagerAdapter);
		mIndicator.setViewPager(mViewPager);
		//实际 这里不需要监听 
		mIndicator.setOnPageChangeListener(new OnPageChangeListener() {	
			@Override
			public void onPageSelected(int position) {	
			}
			
			@Override
			public void onPageScrolled(int arg0, float arg1, int arg2) {
			}
			
			@Override
			public void onPageScrollStateChanged(int arg0) {
			}
		});
	}
	
	@Override
	public boolean onNavigateUp() {
		finish();
		return super.onNavigateUp();
	}
}
